<?php

$mcrp = './../';
include_once('./../functions/session.php');
include_once('./../functions/contests.php');
include_once('./../functions/categories.php');

if($mcuser->isadmin == 0) 
{
	header('Location: ../');
	exit;
}

include('./../skin/default.php');
include('./../skin/header.php');

if(isset($_POST['modify']) && isset($_GET['id']))
{
	$c = GetContestById($_GET['id']);
	$c->title = $_POST['title'];
	$c->category = $_POST['category'];
	$c->description = $_POST['descr'];
	$c->description_en = $_POST['descr_en'];
	$c->variables = $_POST['variables'];
	$c->points = (int)$_POST['points'];
	$c->validationcount = (int)$_POST['validationcount'];
	$c->visible = isset($_POST['visible']);
	UpdateContest($c);
	if($_POST['oldcategory'] != $_POST['category'])
		UpdateContestCategory($c, $_POST['oldcategory']);

	/*$dir = $mcrp . 'contests/' . $c->id . '/';
	$f = fopen($dir . 'validation.php', 'w');
	if($f)
		fwrite($f, $_POST['validationfile']);
	fclose($f);
	$f = fopen($dir . 'contest.php', 'w');
	if($f)
		fwrite($f, $_POST['contestfile']);
	fclose($f);*/
}
else if(isset($_POST['delete']) && isset($_GET['id']))
{
	DeleteContest($_GET['id']);
	header('Location: contests.php');
}
else if(isset($_POST['news']) && isset($_GET['id']))
{
	AddLastContest($_GET['id']);
}

if(isset($_GET['id']))
{
	$c = GetContestById($_GET['id']);
	if($c === false)
	{
		$Error = 'Concours inexistant';
		unset($_GET['id']);
	}
	else
	{
		$Categories = GetAllCategories();
		$dir = $mcrp . 'contests/' . $c->id . '/';
		$f = fopen($dir . 'validation.php', 'r');
		if($f)
		{
			if(filesize($dir . 'validation.php') != 0)
				$validation_php_content = fread($f, filesize($dir . 'validation.php'));
			else
				$validation_php_content = '';
		}
		fclose($f);
		$f = fopen($dir . 'contest.php', 'r');
		if($f)
		{
			if(filesize($dir . 'contest.php') != 0)
				$contest_php_content = fread($f, filesize($dir . 'contest.php'));
			else
				$contest_php_content = '';
		}
		fclose($f);
	}
}
else
{
	$Error = 'Aucun concours sélectionné';
}

?>

<div class="mc_box">
	<h3>Modifier un concours - <a href="<?php echo MC_ROOT . 'contest.php?id=' . $c->id; ?>"><?php echo stripslashes($c->title); ?></a> - <a href="test.php?id=<?php echo $c->id; ?>">Tester</a></h3>
	<div class="mc_contest_view_text">
		<?php if(isset($_GET['id']))
		{
		?>
		<form class="mc_connection_form" method="post">
			<table>
				<tr>
					<td><label for="category">Catégorie :</label></td>
					<td><select name="category" class="mc_input_box">
						<?php
						foreach($Categories as $Cat)
						{
							echo '<option value="' . $Cat->id . '" ' . ($Cat->id == $c->category?'selected="selected"':'') . '>' . stripslashes($Cat->title) . '</option>';
						}
						?>
					</select></td>
				</tr>
				<tr>
					<td><label for="title">Titre :</label></td>
					<td><input id="mc_title" class="mc_input_box" type="text" tabindex="1" name="title" size="30" value="<?php echo stripslashes($c->title); ?>" /><br /></td>
				</tr>
				<tr>
					<td><label for="descr">Description :</label></td>
					<td><textarea id="mc_descr" class="mc_input_box" tabindex="2" name="descr" rows="8" cols="80"><?php echo stripslashes($c->description); ?></textarea></td>
				</tr>
				<tr>
					<td><label for="descr_en">Description anglaise :</label></td>
					<td><textarea id="mc_descr_en" class="mc_input_box" tabindex="3" name="descr_en" rows="8" cols="80"><?php echo stripslashes($c->description_en); ?></textarea></td>
				</tr>
				<tr>
					<td><label for="variables">Variables :</label></td>
					<td><textarea id="mc_variables" class="mc_input_box" tabindex="4" name="variables" rows="4" cols="80"><?php echo stripslashes($c->variables); ?></textarea></td>
				</tr>
				<tr>
					<td><label for="contestfile">Fichier contest.php :</label></td>
					<td><textarea id="mc_contestfile" class="mc_input_box" tabindex="5" name="contestfile" rows="8" cols="80" readonly><?php echo $contest_php_content; ?></textarea></td>
				</tr>
				<tr>
					<td><label for="validationfile">Fichier validation.php :</label></td>
					<td><textarea id="mc_validationfile" class="mc_input_box" tabindex="6" name="validationfile" rows="8" cols="80" readonly><?php echo $validation_php_content; ?></textarea></td>
				</tr>
				<tr>
					<td><label for="points">Points :</label></td>
					<td><input id="mc_points" class="mc_input_box" type="text" tabindex="7" name="points" size="10" value="<?php echo $c->points; ?>" /><br /></td>
				</tr>
				<tr>
					<td><label for="validationcount">Nombre de validations :</label></td>
					<td><input id="mc_validationcount" class="mc_input_box" type="text" tabindex="8" name="validationcount" size="10" value="<?php echo $c->validationcount; ?>" /><br /></td>
				</tr>
				<tr>
					<td><label for="visible">Visible :</label></td>
					<td><input type="checkbox" name="visible" tabindex="9" <?php if($c->visible) echo 'checked="checked"'; ?> /></td>
				</tr>
				<tr>
					<td colspan="2">
						<input type="submit" name="modify" tabindex="10" value="Modifier" />
						<input type="submit" name="news" tabindex="11" value="Nouvelle épreuve" />
						<input type="submit" name="delete" tabindex="12" value="Supprimer (irréversible)" />
					</td>
				</tr>
			</table>
			<input type="hidden" name="oldcategory" value="<?php echo $c->category ?>" />
		</form>
		<?php
		}
		else
		{
			echo "<font color=\"red\"><b>$Error</b></font>";
		}
		?>
	</div>
</div>

<?php
include('./../skin/footer.php');
?>